R Markdown

This is an R Markdown document themed with {bslib} package. {bslib} makes it easy to customize the main colors and fonts of a html_document, flexdashboard::flex_dashboard, shiny::fluidPage(), or more generally any website that uses Bootstrap for styling. The theme parameter in the yaml front-matter of this Rmd document describes a bslib::bs_theme() object, which provides access to 100s of theming options (via its ... argument) in addition to the main options demonstrated here (e.g., bg, fg, primary, etc).

This particular example uses {bslib}’s default Bootstrap version (which, at the time of writing, is Bootstrap 5). However, if reproducibility is important, it’s recommended that you “lock-in” the version by adding version: 5 to the theme definition.

Themed Plots

When running this document with {thematic} installed, the thematic::thematic_rmd(font = "auto") effectively translates theme (CSS) settings to new global theming defaults for {ggplot2}, {lattice}, and {base} R graphics:

ggplot2

library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() + geom_smooth() |> 
  suppressMessages()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

lattice

lattice::show.settings()

base

plot(pressure, col = thematic::thematic_get_option("accent"))

Supported BS widgets

DT

DT::datatable(datasets::anscombe)

GGplotly

g <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  geom_smooth()
plotly::ggplotly(g)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

echarts4r

library(echarts4r)
data <- expand.grid(
  x = seq(-3, 3, by = 0.05),
  y = seq(-3, 3, by = 0.05)
) |> 
  dplyr::mutate(z = sin(x * x + y * y) * x / 3.14)
data |> 
  e_charts(x) |> 
  e_surface(y, z, wireframe = list(show = FALSE)) |> 
  e_visual_map(z)
library(echarts4r)
les <- jsonlite::fromJSON("https://gist.githubusercontent.com/tyluRp/0d7a53f2a1f55cb3c6ffe22c67618267/raw/0684a839c3e49dac1157721ddd906eff8f9491d4/les-miserables.json")
e_charts() |> 
  e_graph(
    layout = "circular", 
    circular = list(
      rotateLabel = TRUE
    ),
    roam = TRUE,
    lineStyle = list(
      color = "source",
      curveness = 0.3
    ),
    label = list(
      position = "right",
      formatter = "{b}"
    )
  ) |>
  e_graph_nodes(
    nodes = les$nodes, 
    names = name, 
    value = value, 
    size = size, 
    category = grp
  ) |> 
  e_graph_edges(
    edges = les$edges, 
    source = from,
    target = to
  ) |>
  e_tooltip()

kableextra

library(kableExtra)
mtcars[1:5, 1:6] %>%
  kbl() %>%
  kable_styling()
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

Not supported yet

Plotly

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
fig <- plot_ly(z = ~volcano, type = "contour")
fig
x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)
fig <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
fig

Reactable

reactable::reactable(datasets::beaver1)

ggiraph

library(ggplot2)
library(ggiraph)
data <- mtcars
data$carname <- row.names(data)
gg_point = ggplot(data = data) +
    geom_point_interactive(aes(x = wt, y = qsec, color = disp,
    tooltip = carname, data_id = carname)) + 
  theme_minimal()
girafe(ggobj = gg_point)